// 获取主表数据逻辑

import ajaxMainTableData from '../../request-functions/getMainTableData';
import deepCopy from '../../../../public/functions/deep-copy';

import language from '../../language';

export default function getMainTableData() {
    const {
        searchModalValue,
        currentSelectDepartment,
        humanResourceOrganizationValue,
        queryOid,
        include_child_depts,
        include_child_orgs,
        pageInfo,
        show_on_job_psn,
        show_reject_offer,
        show_refuse_entry,
        include_entry_apply
    } = this.state;

    const {
        editTable
    } = this.props;


    let postData = {
        querycondition: {
            logic: 'and',
            conditions: []
        },
        queryAreaCode: "ncc600704query",
        querytype: "tree",
        ...searchModalValue,
        tree_node_id: currentSelectDepartment,
        node_type: "ORG_NODE",
        pk_org: humanResourceOrganizationValue.refpk,
        oid: queryOid,
        func_type: 1,
        show_all_info: false,
        show_on_job_psn: show_on_job_psn,
        include_child_depts: include_child_depts,
        include_child_orgs: include_child_orgs,
        pageInfo: pageInfo,
        show_reject_offer: show_reject_offer,
        show_refuse_entry: show_refuse_entry,
        include_entry_apply: include_entry_apply
    };

    return ajaxMainTableData(postData)
        .then((res) => {
            if (res.success) {
                if (!res.data) {
                    res.data = {
                        'ncc600704list': {
                            rows: []
                        }
                    };
                }
                // 主表里排序值如果不设置会是9999999，所以需要手动额把它置空
                res.data['ncc600704list'].rows.map((item) => {
                    if (item.values['showorder'].value === '9999999') {
                        item.values['showorder'].value = '';
                    }
                });
                // 将数据填充到主表

                editTable.setTableData('ncc600704list', deepCopy(res.data['ncc600704list']), false);
                editTable.focusRowByIndex('ncc600704list', 0);

                let maxLength = Math.max(pageInfo.pageSize, res.data['ncc600704list'].rows.length);
                maxLength = maxLength < 10 ? 10 : maxLength;
                this.setState({
                    hasSearched: true,
                    pageInfo: res.data['ncc600704list'].pageInfo || {
                        ...pageInfo,
                        pageIndex: 1,
                        total: 0,
                        totalPage: 1
                    },
                    mainTableData: res.data['ncc600704list'],
                    clickRow: res.data['ncc600704list'] && res.data['ncc600704list'].rows[0] ? {
                        index: 0,
                        record: res.data['ncc600704list'].rows[0]
                    } : null,
                    mainTableHeight: maxLength * 30
                });
                // 主表渲染完了之后，释放主表的按钮
                this.setButtonStatus2('mainHadData');
            }
        });
}
